<?php
/**
 * 备忘录模式
 * This file is part of pattern.
 * Author: tangzhou
 * Date: 2016/9/22
 * Time: 11:33
 * File: Memo.php
 */


class GmaeRole
{
    public $level;
    public $money;

    public function saveState()
    {
        return new RoleSaveState($this->level, $this->money);
    }

    public function getInitSate()
    {
        $this->level = 100;
        $this->money = 100;
    }

    /**
     * 恢复保存的状态
     * @param RoleSaveState $r
     */
    public function RecoveryState(RoleSaveState $r)
    {
        $this->level = $r->level;
        $this->money = $r->money;
    }

    public function displaySate()
    {
        echo '级数:'.$this->level.'--金钱:'.$this->money;
    }

    public function BeenAttack()
    {
        $this->money-=50;
        $this->level-=10;
        if ($this->money < 0 ) {
            echo "赔光了";
        }
    }

}

/**
 * 存储状态
 * Class RoleSaveState
 */
class RoleSaveState
{
    public $level;
    public $money;

    public function __construct($level, $money)
    {
        $this->level = $level;
        $this->money = $money;
    }
}

/**
 * 管理
 * Class RoleManage
 */
class RoleManage
{
    public $role;
}


$start = new GmaeRole();
$start->getInitSate();

//把当前状态保存到role中
$manage = new RoleManage();
$manage->role = $start->saveState();

$start->BeenAttack();
$start->displaySate();

$start->RecoveryState($manage->role);

